﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using EShop.Model;
using EShop.Common;

namespace EShop.DAL
{
    public class MerchandiseOrderDAL:BaseDAL
    {
        /// <summary>
        /// 更新商品销售数量
        /// </summary>
        /// <param name="strBarcode"></param>
        /// <param name="iNum"></param>
        /// <returns></returns>
        public bool UpdateMerchandiseSellQty(string strBarcode, int iNum)
        {
            string strUSP = "USP_MerchandiseSee_UpdateQuantity";
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@Barcode",strBarcode),
                                            new SqlParameter("@Num",iNum)
                                        };
                return DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters) > 0 ? true : false;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 自动上架
        /// 0，操作失败
        /// 1，操作成功
        /// 2，该商品已无库存
        /// </summary>
        /// <param name="strBarcode"></param>
        /// <returns></returns>
        public int AutoSell(string strBarcode)
        {
            string strUSP = "USP_Update_MerchandiseSell";
            try
            {
                SqlParameter[] parameters ={
                                           new SqlParameter("@Barcode",SqlDbType.VarChar,30),
                                           new SqlParameter("@Result",SqlDbType.Int)
                                      };
                parameters[0].Value = strBarcode;
                parameters[1].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters);
                //int i = (int)parameters[1].Value;
                return (int)parameters[1].Value;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }
        //USP_Merchandise_Order_ADD

        public string InserMerchandiseOrder(Model.MerchandiseOrder oMerchandiseOrder)
        {
            string strUSP = "USP_Merchandise_Order_ADD";
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@MemberID",SqlDbType.VarChar,20),
                                            new SqlParameter("@PayAmount",SqlDbType.Money),
                                            new SqlParameter("@Discount",SqlDbType.Decimal),
                                            new SqlParameter("@RealAmount",SqlDbType.Money),
                                            new SqlParameter("@EmployeeID",SqlDbType.VarChar,12),
                                            new SqlParameter("@OrderRemark",SqlDbType.Text)
                                        };
                parameters[0].Value = oMerchandiseOrder.MemberID;
                parameters[1].Value = oMerchandiseOrder.PayAmount;
                parameters[2].Value = oMerchandiseOrder.Discount;
                parameters[3].Value = oMerchandiseOrder.RealAmount;
                parameters[4].Value = oMerchandiseOrder.EmployeeID;
                parameters[5].Value = oMerchandiseOrder.Remark;
                return DbHelper.ExecuteDataSetSQL(strUSP, CommandType.StoredProcedure, parameters).Tables[0].Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        public bool InsertMerchandiseOrderDeatil(Model.MerchandiseOrderDetail oMerchandiseOrderDetail)
        {
            string strUSP = "USP_Merchandise_OrderDetail_ADD";
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@SerialNumber",SqlDbType.VarChar,20),
                                            new SqlParameter("@OrderID",SqlDbType.Int),
                                            new SqlParameter("@Barcode",SqlDbType.VarChar,30),
                                            new SqlParameter("@StockInOrder",SqlDbType.Int),
                                            new SqlParameter("@BuyNumber",SqlDbType.Int),
                                            new SqlParameter("@SellPrice",SqlDbType.Money),
                                            new SqlParameter("@SaleAmount",SqlDbType.Money)
                                        };
                parameters[0].Value = oMerchandiseOrderDetail.SerialNumber;
                parameters[1].Value = oMerchandiseOrderDetail.OrderID;
                parameters[2].Value = oMerchandiseOrderDetail.Barcode;
                parameters[3].Value = oMerchandiseOrderDetail.StockInOrder;
                parameters[4].Value = oMerchandiseOrderDetail.BuyNumber;
                parameters[5].Value = oMerchandiseOrderDetail.SellPrice;
                parameters[6].Value = oMerchandiseOrderDetail.SalePrice;
                return DbHelper.ExecuteNonQuerySQL(strUSP, CommandType.StoredProcedure, parameters) > 0 ? true : false;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }
    }
}